Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: public commands API #232

Merged
merged 20 commits into from
Aug 1, 2023
Merged

cli: public commands API #232

merged 20 commits into from
Aug 1, 2023

Conversation

anpep
Copy link
Collaborator

@anpep anpep commented Jun 5, 2023

Introduce a public API for well-known Pebble-based applications to use.
This PR introduces two basic changes on top of those included in #226:

  • Export the cli.AddCommand() function, so that applications can add new commands.
  • Export the cli.HelpCategories variable, so that applications can add new sections to the help manual.
// Example: adding a help category
package main

import "github.com/canonical/pebble/internals/cli"

var myCategory = cli.HelpCategory {
    Label: "My category",
    Description: "Description for my help category",
    Commands: []string{"new-command"}
}

func main() {
    cli.HelpCategories = append(cli.HelpCategories, myCategory)
    cli.Run()
}
// Example: adding a new command
package main

import (
    "fmt"

    "github.com/canonical/go-flags"
    "github.com/canonical/pebble/internals/cli"
)

type cmdNewCommand struct {
    Force bool `short:"f" long:"force"`
}

func init() {
    cli.AddCommand(&cli.CmdInfo{
        Name:        "new-command",
        Summary:     "Lorem ipsum. Foo bar.",
        Description: "Dolor sit amet.",
        Builder:     func() flags.Commander { return &cmdNewCommand{} },
        ArgsHelp: map[string]string{
            "--force": "Forces the operation",
        },
    })
}

func (cmd *cmdNewCommand) Execute(args []string) error {
    if len(args) > 0 {
        return cli.ErrExtraArgs
    }
    fmt.Fprintf(cli.Stdout, "Hello, world!\n")
    return nil
}

@anpep anpep requested review from flotter and atesburak June 5, 2023 10:33
@anpep anpep self-assigned this Jun 5, 2023
@anpep anpep requested a review from paul-rodriguez June 5, 2023 10:35
internals/cli/cli.go Outdated Show resolved Hide resolved
Copy link

@atesburak atesburak left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minimal change to make AddCommand public, looks good to me with an extremely minor comment.

@paul-rodriguez
Copy link
Contributor

Logical follow-up step on #226, looks good to me.

internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cmd_help.go Outdated Show resolved Hide resolved
@anpep anpep marked this pull request as ready for review June 7, 2023 15:53
@anpep anpep requested a review from niemeyer June 7, 2023 15:54
@anpep anpep added the High Priority Look at me first label Jun 7, 2023
Copy link
Contributor

@flotter flotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the last change required on the Pebble side before we can create a different variant of Pebble in a separate repository.

Some work remains to allow for personality changes to update string references to "pebble" in the help, but that will be a followup PR.

Copy link
Contributor

@niemeyer niemeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks mostly okay since it's a private use, but I still hope we can improve it slightly as outlined below. Bonus points if there's a test for it.

internals/cli/cli.go Show resolved Hide resolved
 * AddHelpCategory() was removed in favor of an exported
   HelpCategories global.
 * AddCommand() now accepts a CmdInfo struct as its sole
   argument.
Copy link
Contributor

@niemeyer niemeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for moving it forward. There are still remaining issues related to the points made in the prior review it seems.

internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
 * Removed unused struct members such as Aliases and Hidden.
 * Replace the Extra function with PassAfterNonOption, as this
   is the only internal flags.Command option that justified its
   need.
 * Rename OptDesc/ArgDesc for clarity.
 * Document the public API.
Copy link
Contributor

@flotter flotter left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some subjective preferences, but otherwise looks great!

internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/last.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cmd_add.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
@anpep anpep added High Priority Look at me first and removed Reviewed labels Jul 3, 2023
Copy link
Contributor

@niemeyer niemeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are a few extra details, but it looks like the points discussed last time about debug commands still haven't been touched?

internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Show resolved Hide resolved
internals/cli/cmd_enter.go Show resolved Hide resolved
internals/cli/cli.go Show resolved Hide resolved
@anpep anpep requested a review from niemeyer July 3, 2023 14:06
@anpep anpep changed the title Public CLI API cli: public commands API Jul 4, 2023
internals/cli/cli.go Outdated Show resolved Hide resolved
The following flags will pass: `-a`, `--long-flag`, `--b`, `--this-is-a-flag`;
the following flags won't: `---`, `---c`, `--quasi--valid`
Copy link
Contributor

@niemeyer niemeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks Angel. One more pass.

internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
internals/cli/cmd_mkdir.go Outdated Show resolved Hide resolved
internals/cli/cmd_restart.go Outdated Show resolved Hide resolved
internals/cli/cmd_rm.go Outdated Show resolved Hide resolved
internals/cli/cmd_run.go Show resolved Hide resolved
internals/cli/cmd_version.go Outdated Show resolved Hide resolved
Copy link
Contributor

@niemeyer niemeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Almost there!

internals/cli/cli.go Show resolved Hide resolved
internals/cli/cli.go Outdated Show resolved Hide resolved
Copy link
Contributor

@niemeyer niemeyer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for all the work, Angel. Hopefully this is ready to go in now.

@jnsgruk jnsgruk merged commit 37a6155 into canonical:master Aug 1, 2023
18 checks passed
@benhoyt benhoyt mentioned this pull request Sep 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
High Priority Look at me first
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants